using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;

namespace CustomSecurity
{
    public sealed class CustomPrincipal : System.Security.Principal.IPrincipal
    {
        private string[] _Roles;
        private string[] _Functions;
        private IIdentity _Identity;

        public CustomPrincipal(IIdentity identity, string[] roles)
        {
            if (identity == null) throw new ArgumentNullException("identity", "Argument must not be null");
            _Identity = identity;
            _Roles = roles;
            _Functions = null;

        }

        public CustomPrincipal(System.Security.Principal.IIdentity identity, string[] roles, string[] functions)
        {
            if (identity == null) throw new ArgumentNullException("identity", "Argument must not be null");
            _Identity = identity;
            _Roles = roles;
            _Functions = functions;
        }

        public CustomPrincipal(CustomIdentity identity )
        {
            if (identity == null)
                throw new ArgumentNullException(
                    "identity",
                    "Argument must not be null"
                    );

            _Identity = identity;

            //// Find the roles for the user.
            //DataSet ds = UserRoleManager.FindByUser(
            //    identity.UserID
            //    );

            //// Create an array to contain the role names.
            //int roleCount = ds.Tables[0].Rows.Count;
            //_Roles = new string[roleCount];

            //// Loop and copy the role names.
            //for (int x = 0; x < roleCount; x++)
            //    _Roles[x] = (string)ds.Tables[0].Rows[x]["role_name"];

            //// Get the collection of functions.
            //_Functions = SecurityManager.EffectiveFunctions(
            //    identity.UserID);
        } 

        public bool IsInRole(string roleName)
        {
            return _Roles != null && Array.IndexOf(_Roles, roleName) >= 0;
        } 

        public System.Security.Principal.IIdentity Identity
        {
            get { return _Identity; }
        }

        public bool IsAuthorized(string functionName)
        {
            return _Functions != null && Array.IndexOf(_Functions, functionName) >= 0;
        } 
    } 
}
